package com.zmn.mcc.admin.controller.common;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.admin.controller.dept.DeptBaseController;
import com.zmn.mcc.model.dto.BodyDIO;
import com.zmn.mcc.model.entity.organize.McOrganize;
import com.zmn.mcc.model.entity.organize.McOrganizeQuery;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/05/07 18:26
 */
@Controller
@RequestMapping("common/organize/")
public class CommonOrganizeListController extends DeptBaseController {

    /**
     * 组织联动
     *
     * @param request
     * @param response
     * @param parentId
     */
    @RequestMapping(value = "listChilds")
    @ResponseBody
    public ResponseDTO listChilds(HttpServletRequest request, HttpServletResponse response,
                                  @RequestBody BodyDIO<McOrganizeQuery> body) {

        if (body == null || body.getBody() == null) {
            return ResponseDTO.fail("空的组织列表");
        }

        McOrganizeQuery query = body.getBody();
        if (NumberUtil.isNullOrZero(query.getParentId())) {
            query.setParentId(GlobalConsts.TOP_ID);
        }

        List<McOrganize> organizes = organizeService.listByParentId(query.getParentId(), GlobalDict.STATUS_ENABLE);

        List<VtDTO> items = new ArrayList<>();
        organizes.forEach(item -> {
            items.add(new VtDTO(item.getOrgId(), item.getOrgName()));
        });

        return ResponseDTO.success(items, String.format("共%s个组织", items.size()));
    }
}
